<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>新增借用紀錄</title>
</head>
<body>
<% if(session.getAttribute("access") == "y" ){
	ResultSet rs;
	%>
<jsp:useBean id="mydb" scope="page" class="bean.ConDbBean"/> <%//連結資料庫的bean %>
<%
	if(request.getParameter("c_pf")!=null){//資料是由預借轉成借用
		String sql="";
		//out.print(request.getParameter("book_id"));
		//out.print(request.getParameter("c_pf"));
  		sql="select * from BOOK WHERE BOOK_ID='"+request.getParameter("book_id")+"'";
		rs=mydb.executeQuery(sql);
		rs.next();
//out.println("fid="+fid);
//out.println("bdate="+bdate);
//out.println("btime="+btime);
//out.println("mid"+mid);
//out.println("bps="+bps);
//out.println("breason="+breason);
//out.println("bp="+bp);
		sql="INSERT INTO BORROW(M_ID,PF_ID,BORROW_REASON,BORROW_PERSON,BORROW_TIME,BORROW_DATE,MUST_RETURN_DATE) VALUES('"+rs.getString("M_ID")+"','"+rs.getString("PF_ID")+"','"+rs.getString("BOOK_REASON")+"','"+session.getAttribute("accessid")+"','"+rs.getString("BOOK_TIME")+"','"+rs.getString("APPLY_DATE")+"','"+rs.getString("APPLY_DATE")+"')";
		mydb.executeInsert(sql);
		if(request.getParameter("c_pf").equalsIgnoreCase("1")){//為場地借用
			sql="UPDATE PLACE SET P_STATE='10' WHERE P_ID='"+rs.getString("PF_ID")+"'";
			mydb.executeUpdate(sql);
		}else if(request.getParameter("c_pf").equalsIgnoreCase("2")){//為設備借用
			sql="UPDATE FACILITY SET F_STATE='10' WHERE F_ID='"+rs.getString("PF_ID")+"'";
			mydb.executeUpdate(sql);
		}
		sql="DELETE FROM BOOK WHERE BOOK_ID='"+request.getParameter("book_id")+"'"; 
    	mydb.executeDelete(sql);
	}
	else{//直接借用
		String pfid = new String(request.getParameter("pf_id").getBytes("iso-8859-1"),"utf-8"); //防止亂碼
		String bdate=new String(request.getParameter("borrow_date").getBytes("iso-8859-1"),"utf-8");
		String btime=new String(request.getParameter("bt_class").getBytes("iso-8859-1"),"utf-8");
		String mid= new String(request.getParameter("m_id").getBytes("iso-8859-1"),"utf-8");
		String bps= new String(request.getParameter("ps").getBytes("iso-8859-1"),"utf-8");
		String breason= new String(request.getParameter("reason").getBytes("iso-8859-1"),"utf-8");
		String bp= new String(request.getParameter("b_p").getBytes("iso-8859-1"),"utf-8");
		String sql="INSERT INTO BORROW(M_ID,PF_ID,BORROW_REASON,BORROW_PERSON,PS,BORROW_TIME,BORROW_DATE,MUST_RETURN_DATE) VALUES('"+mid+"','"+pfid+"','"+breason+"','"+bp+"','"+bps+"','"+btime+"','"+bdate+"','"+bdate+"')";
		mydb.executeInsert(sql);
		//F_STATE=10 表示"借用中"
		if(request.getParameter("pf_c").equalsIgnoreCase("1")){//直接借用場地
			sql="UPDATE PLACE SET P_STATE='10' WHERE P_ID='"+pfid+"'";
		}else if(request.getParameter("pf_c").equalsIgnoreCase("2")){//直接借用設備
			sql="UPDATE FACILITY SET F_STATE='10' WHERE F_ID='"+pfid+"'";
		}
		
		mydb.executeUpdate(sql);
	}
	response.sendRedirect("lookBorrow.jsp"); //直接跳頁到lookBorrow.jsp
  }
  else{
  	response.sendRedirect("../index.jsp"); //若沒有登入則回到index.jsp
  } 
  %>
</body>
</html>